﻿using DevExpress.XtraEditors;
using DevExpress.XtraLayout;
using Inaction.UI.Common;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;

namespace Inaction.UI.Settings
{
    internal static class LayoutControlSetting
    {
        public static void SetProperties(LayoutControl layoutControl)
        {
            layoutControl.AllowCustomizationMenu = true;
            layoutControl.Margin = new Padding(0);
            layoutControl.OptionsCustomizationForm.ShowLoadButton = false;
            layoutControl.OptionsCustomizationForm.ShowSaveButton = false;
            SetProperties(layoutControl.Items);
        }

        private static void SetProperties(DevExpress.XtraLayout.Utils.BaseItemCollection items)
        {
            if (items == null) return;
            foreach (BaseLayoutItem item in items)
            {
                if (item is LayoutControlGroup)
                {
                    var group = item as LayoutControlGroup;
                    group.Padding = new DevExpress.XtraLayout.Utils.Padding(0);
                    group.Spacing = new DevExpress.XtraLayout.Utils.Padding(0);
                    SetProperties(group.Items);
                }
                if (item is TabbedGroup)
                {
                    var tabbedGroup = item as TabbedGroup;
                    tabbedGroup.Padding = new DevExpress.XtraLayout.Utils.Padding(0);
                    tabbedGroup.Spacing = new DevExpress.XtraLayout.Utils.Padding(0);
                }
                if (item is LayoutControlItem)
                {
                    var c = item as LayoutControlItem;
                    c.SizeConstraintsType = SizeConstraintsType.Default;
                    if (c.Control is BaseEdit)
                    {
                        var edit = c.Control as BaseEdit;
                        var binding = edit.DataBindings.Cast<Binding>().FirstOrDefault(p => p.PropertyName == "EditValue");
                        if (binding != null && binding.DataSource is BindingSource)
                        {
                            var type = BindingHelper.GetListItemType(binding.DataSource as BindingSource);
                            var propertyInfo = type.GetProperty(binding.BindingMemberInfo.BindingField);
                            if (propertyInfo == null) return;

                            var displayName = propertyInfo.GetCustomAttribute<DisplayNameAttribute>();
                            if (displayName != null)
                            {
                                item.Text = displayName.DisplayName;
                            }
                        }
                    }
                    else if (c.Control is LayoutControl)
                    {
                        c.SizeConstraintsType = SizeConstraintsType.Custom;
                        c.MinSize = new System.Drawing.Size(0, 0);
                        c.MaxSize = new System.Drawing.Size(0, 0);
                    }
                }
            }
        }
    }
}
